package com.atguigu.crowd.mvc.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


@Configuration
//开启web的权限控制
@EnableWebSecurity

//启用全局方法权限控制并且设置 prePostEnabled = true。保证@PreAuthority、 @PostAuthority、@PreFilter、@PostFilter 生效
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebAppSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

//    @Bean
//    public PasswordEncoder passwordEncoder(){
//        return new BCryptPasswordEncoder();
//    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication()
//                .withUser("jack")
//                .password("123123")
//                .roles("admin");
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
//                .antMatchers("/admin/login.html")
//                .permitAll()
//                不需要认证的url请求设置
                .antMatchers("/index.jsp")
                .permitAll()
                .antMatchers("/bootstrap/**") // 针对静态资源进行设置，无条件访问
                 .permitAll() // 针对静态资源进行设置，无条件访问
                 .antMatchers("/crowd/**") // 针对静态资源进行设置，无条件访问
                 .permitAll() // 针对静态资源进行设置，无条件访问
                 .antMatchers("/css/**") // 针对静态资源进行设置，无条件访问
                 .permitAll() // 针对静态资源进行设置，无条件访问
                 .antMatchers("/fonts/**") // 针对静态资源进行设置，无条件访问
                 .permitAll() // 针对静态资源进行设置，无条件访问
                 .antMatchers("/img/**") // 针对静态资源进行设置，无条件访问
                 .permitAll() // 针对静态资源进行设置，无条件访问
                 .antMatchers("/jquery/**") // 针对静态资源进行设置，无条件访问
                 .permitAll()
                .antMatchers("/layer/**") // 针对静态资源进行设置，无条件访问
                 .permitAll() // 针对静态资源进行设置，无条件访问
                 .antMatchers("/script/**") // 针对静态资源进行设置，无条件访问
                 .permitAll() // 针对静态资源进行设置，无条件访问
                 .antMatchers("/ztree/**") // 针对静态资源进行设置，无条件访问
                 .permitAll()
                .anyRequest()
                .authenticated()
                .and()
//                自定义登录配置
                .formLogin()
                .loginPage("/admin/login.html")
                .loginProcessingUrl("/security/do/login.html")
                .defaultSuccessUrl("/admin/to/main/page.html")
                .usernameParameter("loginAcct")
                .passwordParameter("userPswd")
                .permitAll()
                .and()
//                退出登录
                .logout()
                .logoutUrl("/security/admin/do/logout.html")
                .logoutSuccessUrl("/admin/login.html")
//                关闭csrf设置
                .and()
                .csrf()
                .disable();

    }
}
